package com.dudu.service.support;

import java.util.List;
import java.util.Map;

import com.dudu.bean.IfControlBean;
import com.dudu.bean.IfControlRuleBean;

/**
 * 信控规则处理器 根据给定的请求信息匹配规则
 * 
 * @author zx
 *
 */
public class InfoControlRuleProcessor {

	private List<IfControlRuleBean> rules;

	public InfoControlRuleProcessor(List<IfControlRuleBean> ifcRules) {
		this.rules = ifcRules;
	}

	/**
	 * 根据请求参数匹配规则
	 * 
	 * @param requestBean
	 * @param alreadyRequestCount
	 * @param parameterMap
	 * @return
	 */
	public IfControlRuleBean match(AbstractInfoControlProcessor processor) {
		Map<String, String> parameterMap = processor.parameterMap;
		IfControlBean requestBean = processor.ifcBean;
		IfControlRuleBean matchedRule = null;
		for (int i = 0, l = rules.size(); i < l; i++) {
			IfControlRuleBean rule = rules.get(i);
			/**
			 * 1)当次发送数量小于最大值 2）当次发送数量+之前发送数量小于最大值 以上两个条件同时满足的规则不匹配
			 */
			if (requestBean.getRequestCount() <= rule.getMaxSendCount()
					&& processor.alreadyRequestCount(requestBean) <= rule.getMaxSendCount()) {
				continue;
			}
			String includeRecord = rule.getDisplaynumRecord();
			String numberSoucre = parameterMap.get("source");
			if ("all".equals(includeRecord)) {// 不需要判断是否做了显号报备
				matchedRule = rule;
				break;
			} else if ("yes".equals(includeRecord) && "custom".equals(numberSoucre)) {// 有报备
				matchedRule = rule;
				break;
			} else if ("no".equals(includeRecord) && !"custom".equals(numberSoucre)) {// 无报备
				matchedRule = rule;
				break;
			}
		}
		return matchedRule;
	}

}
